package tool;

import jdk.internal.util.xml.impl.Input;
import org.junit.Test;
import sun.security.krb5.internal.crypto.Des;

import java.io.*;

/**
 * @ClassName InOutputStream
 * @Description TODO
 * @Author LYJ
 * @Date 2018/12/22 21:37
 * @Verison 1.0
 **/
public class InOutputStream {
    @Test
    public void test01() throws IOException {
        FileInputStream inputStream = new FileInputStream("D:\\HelloWorld.txt");

        byte[] bytes = new byte[1024];

        int read = inputStream.read(bytes, 0, bytes.length);
        int j = 1;
        for (byte aByte : bytes) {
            if(aByte <= 0xf){
                System.out.print(0);
            }
            System.out.print(Integer.toHexString(aByte)+" ");
            if(j++%10==0){
                System.out.println();
            }
        }

        inputStream.close();

       /* FileOutputStream outputStream = new FileOutputStream("D:\\HelloWorld1.txt");

        byte[] bytes = new byte[20];
        int read = inputStream.read(bytes);
        System.out.println(read);
        outputStream.write(bytes,0,11);
        inputStream.close();
        outputStream.close();

        int read = inputStream.read();
        System.out.println(Integer.toHexString(read));
        inputStream.close();*/
    }

    @Test
    public void test02() throws IOException {
        FileInputStream inputStream = null;
        try {
             inputStream = new FileInputStream("D:\\HelloWorld.txt");

            byte[] bytes = new byte[1024];

            int read = 0;
            int j = 1;

            while ((read= inputStream.read(bytes, 0, bytes.length))!=-1){
                for (byte aByte : bytes) {
                    if(aByte<=0xf){
                        System.out.print(0);
                    }
                    System.out.print(Integer.toHexString(aByte)+" ");
                    if(j++%10==0){
                        System.out.println();
                    }
                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                inputStream.close();
            }
        }
    }

    @Test
    public void test03(){
        try {

            FileInputStream inputStream = new FileInputStream("D:\\HelloWorld.txt");
            FileOutputStream outputStream = new FileOutputStream("D:\\HelloWorld2.txt");

            byte[] bytes = new byte[20];
            int read;
            if((read = inputStream.read(bytes, 0, bytes.length))!=-1){
                outputStream.write(bytes,0,bytes.length);
            }

            inputStream.close();
            outputStream.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void test04(){
        try {

            FileInputStream inputStream = new FileInputStream("D:\\HelloWorld.txt");
            FileOutputStream outputStream = new FileOutputStream("D:\\HelloWorld2.txt");

            int read;

            while ((read = inputStream.read())!=-1){
                outputStream.write(read);
            }



            inputStream.close();
            outputStream.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void test06(){
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\HelloWorld.txt");
            byte[] bytes = new byte[1024];
            int read = fileInputStream.read(bytes);
            System.out.println(read);
            fileInputStream.close();
           /* ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            FileOutputStream fileOutputStream = new FileOutputStream("D:\\HelloWorld2.txt");
            int read;

            while ((read = fileInputStream.read())!=-1){
                outputStream.write(read);
            }

            System.out.println(outputStream.toString());
            outputStream.writeTo(fileOutputStream);

            fileInputStream.close();
            outputStream.close();
            fileOutputStream.close();*/


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test07() throws IOException {

        File file = new File("D:\\Hello.txt");
        file.createNewFile();
        System.out.println(file);
        /*try {
            FileOutputStream outputStream = new FileOutputStream("D:\\HelloWorld2.txt");
            FileInputStream inputStream = new FileInputStream("D:\\HelloWorld2.txt");
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeInt(11);
            dataOutputStream.writeChar('中');
            int i = dataInputStream.readInt();
            char c = dataInputStream.readChar();
            System.out.println(i);
            System.out.println(c);
            outputStream.close();
            dataInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/



    }

    @Test
    public void test08() throws IOException {
        FileInputStream inputStream = new FileInputStream("D:\\Hello.txt");
        byte[] bytes = new byte[1024];

        int read = inputStream.read();
        System.out.println(read);
        inputStream.close();
    }

    private static final String SRCFILE = "D:\\Hello.txt";
    private static final String DESTFILE = "D:\\Hllo2.txt";

    @Test
    public void testDemo() throws IOException{
        FileInputStream fileInputStream = new FileInputStream(SRCFILE);
        FileOutputStream fileOutputStream = new FileOutputStream(DESTFILE);
        byte[] bytes = new byte[1024];
        int read;
        while((read = fileInputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,read);
            fileOutputStream.flush();
        }
        fileInputStream.close();
        fileOutputStream.close();
    }


    @Test
    public void testDemo2() throws IOException{
        FileInputStream fileInputStream = new FileInputStream(SRCFILE);
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(DESTFILE);

        byte[] bytes = new byte[1024];
        int read;
        while((read = fileInputStream.read(bytes))!=-1){
            arrayOutputStream.write(bytes,0,read);
            arrayOutputStream.writeTo(fileOutputStream);
            fileOutputStream.flush();
        }
        fileInputStream.close();
        arrayOutputStream.close();
    }



    @Test
    public void testDemo3() throws IOException{
        FileInputStream fileInputStream = new FileInputStream(SRCFILE);
        FileOutputStream fileOutputStream = new FileOutputStream(DESTFILE);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        byte[] bytes = new byte[1024];
        int read;
        while((read = bufferedInputStream.read(bytes,0,bytes.length))!=-1){
            bufferedOutputStream.write(bytes,0,read);
            fileOutputStream.flush();
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }


    @Test
    public void testDemo4() throws IOException{
        FileInputStream fileInputStream = new FileInputStream(DESTFILE);
        FileOutputStream fileOutputStream = new FileOutputStream(DESTFILE);
        DataInputStream dataInputStream = new DataInputStream(fileInputStream);
        DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);

        dataOutputStream.writeChars("六六六六六");
        char c1 = dataInputStream.readChar();
        char c2 = dataInputStream.readChar();
        char c3 = dataInputStream.readChar();
        char c4 = dataInputStream.readChar();
        System.out.print(c1);
        System.out.print(c2);
        System.out.print(c3);
        System.out.print(c4);


        dataInputStream.close();
        dataOutputStream.close();
    }


    @Test
    public void testDemo5() throws IOException{
        InputStreamReader inputStreamReader = new InputStreamReader(
                new FileInputStream(SRCFILE),"utf8"
        );

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                new FileOutputStream(DESTFILE)
        );


        char[] chars = new char[1024];
        int read;
        while((read = inputStreamReader.read(chars,0,chars.length))!=-1){
            outputStreamWriter.write(chars);
        }

        inputStreamReader.close();
        outputStreamWriter.close();
    }


    @Test
    public void testDemo6() throws IOException{
       FileReader reader = new FileReader(SRCFILE);

       FileWriter writer = new FileWriter(DESTFILE);



        char[] chars = new char[1024];
        int read;
        while((read = reader.read(chars,0,chars.length))!=-1){
            writer.write(chars,0,read);
            writer.flush();
        }

        reader.close();
        writer.close();
    }


    @Test
    public void testDemo7() throws IOException{
        BufferedReader reader = new BufferedReader(
                new FileReader(SRCFILE)
        );

        BufferedWriter writer = new BufferedWriter(
                new FileWriter(DESTFILE)
        );



        String str;
        while((str = reader.readLine())!=null){
            writer.write(str);
            writer.newLine();
            writer.flush();
        }

        reader.close();
        writer.close();
    }


    @Test
    public void testDemo8() throws IOException{
        BufferedReader reader = new BufferedReader(
                new FileReader(SRCFILE)
        );

       PrintWriter writer = new PrintWriter(DESTFILE);



        String str;
        while((str = reader.readLine())!=null){
            writer.println(str);
            writer.flush();
        }


        reader.close();
        writer.close();
    }


}
